﻿#region Copyright Notice
// This file is part of GPL-It.
// Copyright (C) 2008 Fabien Barbier
// 
// GPL-It is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// GPL-It is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace GPL_It
{
	partial class NoticeGenerationForm : Form
	{
		static Regex tagReplaceRegex = new Regex("<(?<name>[a-z]+)>", RegexOptions.Multiline | RegexOptions.Compiled | RegexOptions.CultureInvariant);
		Dictionary<string, string> tagReplaceDictionary = new Dictionary<string,string>();
		MainForm mainForm;

		#region Constructor & Initialization

		public NoticeGenerationForm(MainForm mainForm)
		{
			if (mainForm == null)
				throw new ArgumentNullException("mainForm");
			this.mainForm = mainForm;
			InitializeComponent();
			LoadResources();
			LoadSettings();
		}

		private void LoadResources()
		{
			openFileDialog.Filter = Properties.Resources.TextFileFilter;
			saveFileDialog.Filter = Properties.Resources.TextFileFilter;
		}

		#endregion

		#region Settings Management

		private void LoadSettings()
		{
			nameTextBox.Text = Properties.Settings.Default.ProjectName;
			descriptionTextBox.Text = Properties.Settings.Default.ProjectDescription;
			authorTextBox.Text = Properties.Settings.Default.ProjectAuthor;
			yearTextBox.Text = Properties.Settings.Default.ProjectYear;
			templateTextBox.Text = Properties.Settings.Default.NoticeTemplate;
		}

		private void SaveSettings()
		{
			Properties.Settings.Default.ProjectName = nameTextBox.Text;
			Properties.Settings.Default.ProjectDescription = descriptionTextBox.Text;
			Properties.Settings.Default.ProjectAuthor = authorTextBox.Text;
			Properties.Settings.Default.ProjectYear = yearTextBox.Text;
			Properties.Settings.Default.NoticeTemplate = templateTextBox.Text;
			Properties.Settings.Default.Save();
		}

		#endregion

		private string CreateNotice()
		{
			tagReplaceDictionary.Clear();
			tagReplaceDictionary.Add("program", nameTextBox.Text);
			tagReplaceDictionary.Add("description", descriptionTextBox.Text);
			tagReplaceDictionary.Add("author", authorTextBox.Text);
			tagReplaceDictionary.Add("year", yearTextBox.Text);

			return tagReplaceRegex.Replace(templateTextBox.Text,
				delegate(Match match)
				{
					string result;

					if (tagReplaceDictionary.TryGetValue(match.Groups["name"].Value, out result))
						return result;
					else
						return match.ToString();
				});
		}

		#region Event Handling

		protected override void OnFormClosed(FormClosedEventArgs e)
		{
			SaveSettings();
			base.OnFormClosed(e);
		}

		#region Dialog Buttons

		private void applyButton_Click(object sender, EventArgs e)
		{
			mainForm.HeaderText = CreateNotice();
		}

		private void okButton_Click(object sender, EventArgs e)
		{
			mainForm.HeaderText = CreateNotice();
		}

		private void cancelButton_Click(object sender, EventArgs e)
		{
			LoadSettings();
		}

		#endregion

		#region Template

		#region Buttons

		private void loadTemplateButton_Click(object sender, EventArgs e)
		{
			if (openFileDialog.ShowDialog() == DialogResult.OK)
				templateTextBox.Text = File.ReadAllText(openFileDialog.FileName, Encoding.UTF8);
		}

		private void saveTemplateButton_Click(object sender, EventArgs e)
		{
			if (saveFileDialog.ShowDialog() == DialogResult.OK)
				File.WriteAllText(saveFileDialog.FileName, templateTextBox.Text, Encoding.UTF8);
		}

		private void copyTemplateButton_Click(object sender, EventArgs e)
		{
			Clipboard.Clear();
			if (templateTextBox.Text.Length > 0)
				Clipboard.SetText(templateTextBox.Text, TextDataFormat.UnicodeText);
		}

		#endregion

		#region Tag List

		private void templateListView_DoubleClick(object sender, EventArgs e)
		{
			if (templateListView.SelectedItems.Count > 0)
				templateTextBox.SelectedText = templateListView.SelectedItems[0].Text;
		}

		#endregion

		#endregion

		#endregion
	}
}
